package com.xspace.android.xafcommon.base;

import android.app.Application;
import android.app.Dialog;
import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;

import com.xspace.android.xafcommon.appliction.BaseApplication;
import com.xspace.android.xafcommon.util.BaseRProxy;
import com.xspace.android.xafcommon.util.Constants;
import com.xspace.android.xafcommon.util.PreferencesUtils;
import com.xspace.android.xafcommon.util.StringUtils;
import com.xspace.android.xafcommon.view.dialog.DialogHelp;


public abstract class BaseTabAct extends TabActivity {
	protected ExitAppReceiver exitAppReceiver = new ExitAppReceiver();
	protected Dialog mDialog;
	protected static final Intent SERVICE_INTENT = new Intent();

	/*
	 * <p>Title: onCreate</p> <p>Description: </p>
	 * 
	 * @param savedInstanceState
	 * 
	 * @see android.app.ActivityGroup#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		registerReceiver(exitAppReceiver, new IntentFilter(BaseRProxy.BR_EXIT_APP));
	}

	public void onResume() {
		super.onResume();
//		MobclickAgent.onResume(this);
	}

	public void onPause() {
		super.onPause();
//		MobclickAgent.onPause(this);
	}


	public void showDialog(String showTxt) {
		mDialog =  DialogHelp.getWaitDialog(this, showTxt);
		mDialog.setCancelable(true);
		mDialog.show();
	}

	public void cancelDialog() {
		if (mDialog != null) {
			mDialog.dismiss();
			mDialog = null;
		}
	}

	/*
	 * <p>Title: onBackPressed</p> <p>Description: </p>
	 * 
	 * @see android.app.Activity#onBackPressed()
	 */
	@Override
	public void onBackPressed() {
		finish();
	}

	/*
	 * <p>Title: onStop</p> <p>Description: </p>
	 * 
	 * @see android.app.Activity#onStop()
	 */
	@Override
	protected void onStop() {
		super.onStop();
	}

	/**
	 * 
	 * @Description: 根据用户登录标识判断， 并未根据实际service连接进行判断
	 * @param @return true 登录，false未登录
	 * @return
	 * @throws
	 */
	public boolean isOnline() {
		Application app = getApplication();
		if (app instanceof BaseApplication) {
			BaseApplication application = (BaseApplication) app;
			return application.isAccountConfigured();
		} else {
			return false;
		}
	}

	/*
	 * <p>Title: onDestroy</p> <p>Description: </p>
	 * 
	 * @see android.app.ActivityGroup#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(exitAppReceiver);
	}

	/**
	 * 
	 * @Description: 服务断开处理
	 * @param value
	 * @return
	 * @throws
	 */
	public void processServiceDisconnect(String value) {
	}

	/**
	 * 
	 * @Description: 服务连接处理
	 * @param
	 * @return
	 * @throws
	 */
	public void processServiceConnect() {
	}

	/**
	 * 
	 * @Description: 程序退出处理
	 * @param
	 * @return
	 * @throws
	 */
	public void processExitApp() {
		finish();
	}

	// 后台服务连接状态广播监听
	private class ExitAppReceiver extends BroadcastReceiver {
		/*
		 * <p>Title: onReceive</p> <p>Description: </p>
		 * 
		 * @param context
		 * 
		 * @param intent
		 * 
		 * @see
		 * android.content.BroadcastReceiver#onReceive(android.content.Context,
		 * android.content.Intent)
		 */
		@Override
		public void onReceive(Context context, Intent intent) {
			processExitApp();
			String cmd = intent.getStringExtra("key");
			if (!StringUtils.isEmpty(cmd) && cmd.equals("conflict")) {// 关闭自己
				Toast.makeText(BaseTabAct.this, "你的账号已在另一地点登陆。", Toast.LENGTH_LONG).show();
				PreferencesUtils.setPfValue(BaseTabAct.this, Constants.ACCOUNT_VALIDATED, "0", String.class.getName());
			}
		}
	}
}
